package com.kfm.pm;

public class Demo01 {

    public static void main(String[] args) {
        // 实现二分查找
        int[] arr = {1, 2, 4, 6, 7, 9, 10, 29, 30, 34, 56};
        int key = 34;
        int start = 0;
        int end = arr.length - 1;
        int mid = (start + end) / 2;
//        if (arr[mid] == key) {
//            System.out.println("你要查找的元素所在下标是" + mid);
//        } else if (arr[mid] > key) {
//            end = mid - 1;
//        } else {
//            start = mid + 1;
//        }
//        mid = (start + end) / 2;
//        if (arr[mid] == key) {
//            System.out.println("你要查找的元素所在下标是" + mid);
//        } else if (arr[mid] > key) {
//            end = mid - 1;
//        } else {
//            start = mid + 1;
//        }
//        mid = (start + end) / 2;
//        if (arr[mid] == key) {
//            System.out.println("你要查找的元素所在下标是" + mid);
//        } else if (arr[mid] > key) {
//            end = mid - 1;
//        } else {
//            start = mid + 1;
//        }
        while (end >= start) {
            if (arr[mid] == key) {
                System.out.println(mid);
                break;
            } else if (arr[mid] > key) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
            mid = (end + start) / 2;
        }
        if (start > end) {
            System.out.println(-1);
        }
    }
}
